<template>
  <div class="modern-list-container">
    <!-- 搜索和操作区域 -->
    <div v-if="showFlag" class="modern-list-content">
      <!-- 搜索栏 -->
      <el-form :inline="true" :model="searchForm" class="modern-search-form">
        <div class="search-fields">
          <el-form-item class="search-item">
            <el-input
              v-model="searchForm.username"
              placeholder="用户名"
              clearable
              class="modern-search-input"
              prefix-icon="el-icon-search"
            >
           
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              class="modern-search-btn"
              @click="search"
              :icon="contents.searchBtnIcon ? 'el-icon-search' : ''"
            >
              <span v-if="contents.searchBtnFont">查询</span>
            </el-button>
          </el-form-item>
        </div>
      </el-form>

      <!-- 操作按钮组 -->
      <div class="action-toolbar">
        <el-button-group class="action-group">
          <el-tooltip content="新增" v-if="isAuth('users', '新增')">
            <el-button
              type="success"
              class="modern-action-btn"
              @click="addOrUpdateHandler()"
              :icon="contents.btnAdAllIcon ? 'el-icon-plus' : ''"
            >
              <span v-if="contents.btnAdAllFont">新增</span>
            </el-button>
          </el-tooltip>

          <el-tooltip
            content="删除"
            v-if="isAuth('users', '删除') && contents.tableSelection"
          >
            <el-button
              type="danger"
              class="modern-action-btn"
              :disabled="dataListSelections.length <= 0"
              @click="deleteHandler()"
              :icon="contents.btnAdAllIcon ? 'el-icon-delete' : ''"
            >
              <span v-if="contents.btnAdAllFont">删除</span>
            </el-button>
          </el-tooltip>
        </el-button-group>
      </div>

      <!-- 数据表格 -->
      <div class="modern-table-wrapper">
        <el-table
          :data="dataList"
          class="modern-data-table"
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandler"
        >
          <el-table-column
            v-if="contents.tableSelection"
            type="selection"
            width="48"
            align="center"
          ></el-table-column>

          <el-table-column
            label="索引"
            v-if="contents.tableIndex"
            type="index"
            width="64"
            align="center"
          >
            <template slot-scope="scope">
              <span class="index-badge">{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="username" label="用户名" min-width="180">
            <template slot-scope="scope">
              <div class="table-cell">
                <i class="el-icon-user"></i>
                <span class="text-ellipsis">{{ scope.row.username }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="password" label="密码" min-width="160">
            <template slot-scope="scope">
              <div class="secret-text">
                {{ scope.row.password.replace(/./g, "•") }}
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="role" label="角色" min-width="140">
            <template slot-scope="scope">
              <el-tag :type="roleTagType(scope.row.role)" effect="light">
                {{ scope.row.role }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="180" fixed="right">
            <template slot-scope="scope">
              <div class="table-actions">
                <el-tooltip content="详情" v-if="isAuth('users', '查看')">
                  <el-button
                    type="text"
                    icon="el-icon-document"
                    @click="addOrUpdateHandler(scope.row.id, 'info')"
                    class="action-btn"
                    >{{ contents.tableBtnFont ? "详情" : "" }}</el-button
                  >
                </el-tooltip>

                <el-tooltip content="修改" v-if="isAuth('users', '修改')">
                  <el-button
                    type="text"
                    icon="el-icon-edit"
                    @click="addOrUpdateHandler(scope.row.id)"
                    class="action-btn"
                    >{{ contents.tableBtnFont ? "修改" : "" }}</el-button
                  >
                </el-tooltip>

                <el-tooltip content="删除" v-if="isAuth('users', '删除')">
                  <el-button
                    type="text"
                    icon="el-icon-delete"
                    @click="deleteHandler(scope.row.id)"
                    class="action-btn"
                    >{{ contents.tableBtnFont ? "删除" : "" }}</el-button
                  >
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="modern-pagination">
          <el-pagination
            background
            :page-sizes="[10, 20, 50, 100]"
            :page-size="Number(contents.pageEachNum)"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalPage"
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
          ></el-pagination>
        </div>
      </div>
    </div>

    <!-- 添加/修改弹窗 -->
    <add-or-update
      v-if="addOrUpdateFlag"
      :parent="this"
      ref="addOrUpdate"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./add-or-update.vue";
import styleJs from "../../../utils/style.js";
import utilsJs, {
  getYearFormat,
  getMonthFormat,
  getDateFormat,
  getDatetimeFormat,
} from "../../../utils/utils.js";
export default {
  data() {
    return {
      searchForm: {
        key: "",
      },
      form: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag: false,
      contents: null,
      layouts: "",
    };
  },
  created() {
    this.contents = styleJs.listStyle();
    this.init();
    this.getDataList();
    this.contentStyleChange();
  },
  mounted() {},
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g, "");
    },
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    roleTagType(role) {
      const map = {
        管理员: "danger",
        编辑员: "warning",
        普通用户: "success",
      };
      return map[role] || "info";
    },
    contentStyleChange() {
      this.contentSearchStyleChange();
      this.contentBtnAdAllStyleChange();
      this.contentSearchBtnStyleChange();
      this.contentTableBtnStyleChange();
      this.contentPageStyleChange();
    },
    contentSearchStyleChange() {
      this.$nextTick(() => {
        document
          .querySelectorAll(".form-content .slt .el-input__inner")
          .forEach((el) => {
            let textAlign = "left";
            if (this.contents.inputFontPosition == 2) textAlign = "center";
            if (this.contents.inputFontPosition == 3) textAlign = "right";
            el.style.textAlign = textAlign;
            el.style.height = this.contents.inputHeight;
            el.style.lineHeight = this.contents.inputHeight;
            el.style.color = this.contents.inputFontColor;
            el.style.fontSize = this.contents.inputFontSize;
            el.style.borderWidth = this.contents.inputBorderWidth;
            el.style.borderStyle = this.contents.inputBorderStyle;
            el.style.borderColor = this.contents.inputBorderColor;
            el.style.borderRadius = this.contents.inputBorderRadius;
            el.style.backgroundColor = this.contents.inputBgColor;
          });
        if (this.contents.inputTitle) {
          document
            .querySelectorAll(".form-content .slt .el-form-item__label")
            .forEach((el) => {
              el.style.color = this.contents.inputTitleColor;
              el.style.fontSize = this.contents.inputTitleSize;
              el.style.lineHeight = this.contents.inputHeight;
            });
        }
        setTimeout(() => {
          document
            .querySelectorAll(".form-content .slt .el-input__prefix")
            .forEach((el) => {
              el.style.color = this.contents.inputIconColor;
              el.style.lineHeight = this.contents.inputHeight;
            });
          document
            .querySelectorAll(".form-content .slt .el-input__suffix")
            .forEach((el) => {
              el.style.color = this.contents.inputIconColor;
              el.style.lineHeight = this.contents.inputHeight;
            });
          document
            .querySelectorAll(".form-content .slt .el-input__icon")
            .forEach((el) => {
              el.style.lineHeight = this.contents.inputHeight;
            });
        }, 10);
      });
    },
    // 搜索按钮
    contentSearchBtnStyleChange() {
      this.$nextTick(() => {
        document
          .querySelectorAll(".form-content .slt .el-button--success")
          .forEach((el) => {
            el.style.height = this.contents.searchBtnHeight;
            el.style.color = this.contents.searchBtnFontColor;
            el.style.fontSize = this.contents.searchBtnFontSize;
            el.style.borderWidth = this.contents.searchBtnBorderWidth;
            el.style.borderStyle = this.contents.searchBtnBorderStyle;
            el.style.borderColor = this.contents.searchBtnBorderColor;
            el.style.borderRadius = this.contents.searchBtnBorderRadius;
            el.style.backgroundColor = this.contents.searchBtnBgColor;
          });
      });
    },
    // 新增、批量删除
    contentBtnAdAllStyleChange() {
      this.$nextTick(() => {
        document
          .querySelectorAll(".form-content .ad .el-button--success")
          .forEach((el) => {
            el.style.height = this.contents.btnAdAllHeight;
            el.style.color = this.contents.btnAdAllAddFontColor;
            el.style.fontSize = this.contents.btnAdAllFontSize;
            el.style.borderWidth = this.contents.btnAdAllBorderWidth;
            el.style.borderStyle = this.contents.btnAdAllBorderStyle;
            el.style.borderColor = this.contents.btnAdAllBorderColor;
            el.style.borderRadius = this.contents.btnAdAllBorderRadius;
            el.style.backgroundColor = this.contents.btnAdAllAddBgColor;
          });
        document
          .querySelectorAll(".form-content .ad .el-button--danger")
          .forEach((el) => {
            el.style.height = this.contents.btnAdAllHeight;
            el.style.color = this.contents.btnAdAllDelFontColor;
            el.style.fontSize = this.contents.btnAdAllFontSize;
            el.style.borderWidth = this.contents.btnAdAllBorderWidth;
            el.style.borderStyle = this.contents.btnAdAllBorderStyle;
            el.style.borderColor = this.contents.btnAdAllBorderColor;
            el.style.borderRadius = this.contents.btnAdAllBorderRadius;
            el.style.backgroundColor = this.contents.btnAdAllDelBgColor;
          });
        document
          .querySelectorAll(".form-content .ad .el-button--warning")
          .forEach((el) => {
            el.style.height = this.contents.btnAdAllHeight;
            el.style.color = this.contents.btnAdAllWarnFontColor;
            el.style.fontSize = this.contents.btnAdAllFontSize;
            el.style.borderWidth = this.contents.btnAdAllBorderWidth;
            el.style.borderStyle = this.contents.btnAdAllBorderStyle;
            el.style.borderColor = this.contents.btnAdAllBorderColor;
            el.style.borderRadius = this.contents.btnAdAllBorderRadius;
            el.style.backgroundColor = this.contents.btnAdAllWarnBgColor;
          });
      });
    },
    // 表格
    rowStyle({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        if (this.contents.tableStripe) {
          return { color: this.contents.tableStripeFontColor };
        }
      } else {
        return "";
      }
    },
    cellStyle({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        if (this.contents.tableStripe) {
          return { backgroundColor: this.contents.tableStripeBgColor };
        }
      } else {
        return "";
      }
    },
    headerRowStyle({ row, rowIndex }) {
      return { color: this.contents.tableHeaderFontColor };
    },
    headerCellStyle({ row, rowIndex }) {
      return { backgroundColor: this.contents.tableHeaderBgColor };
    },
    // 表格按钮
    contentTableBtnStyleChange() {
      // this.$nextTick(()=>{
      //   setTimeout(()=>{
      //     document.querySelectorAll('.table-content .tables .el-table__body .el-button--success').forEach(el=>{
      //       el.style.height = this.contents.tableBtnHeight
      //       el.style.color = this.contents.tableBtnDetailFontColor
      //       el.style.fontSize = this.contents.tableBtnFontSize
      //       el.style.borderWidth = this.contents.tableBtnBorderWidth
      //       el.style.borderStyle = this.contents.tableBtnBorderStyle
      //       el.style.borderColor = this.contents.tableBtnBorderColor
      //       el.style.borderRadius = this.contents.tableBtnBorderRadius
      //       el.style.backgroundColor = this.contents.tableBtnDetailBgColor
      //     })
      //     document.querySelectorAll('.table-content .tables .el-table__body .el-button--primary').forEach(el=>{
      //       el.style.height = this.contents.tableBtnHeight
      //       el.style.color = this.contents.tableBtnEditFontColor
      //       el.style.fontSize = this.contents.tableBtnFontSize
      //       el.style.borderWidth = this.contents.tableBtnBorderWidth
      //       el.style.borderStyle = this.contents.tableBtnBorderStyle
      //       el.style.borderColor = this.contents.tableBtnBorderColor
      //       el.style.borderRadius = this.contents.tableBtnBorderRadius
      //       el.style.backgroundColor = this.contents.tableBtnEditBgColor
      //     })
      //     document.querySelectorAll('.table-content .tables .el-table__body .el-button--danger').forEach(el=>{
      //       el.style.height = this.contents.tableBtnHeight
      //       el.style.color = this.contents.tableBtnDelFontColor
      //       el.style.fontSize = this.contents.tableBtnFontSize
      //       el.style.borderWidth = this.contents.tableBtnBorderWidth
      //       el.style.borderStyle = this.contents.tableBtnBorderStyle
      //       el.style.borderColor = this.contents.tableBtnBorderColor
      //       el.style.borderRadius = this.contents.tableBtnBorderRadius
      //       el.style.backgroundColor = this.contents.tableBtnDelBgColor
      //     })
      //   }, 50)
      // })
    },
    // 分页
    contentPageStyleChange() {
      let arr = [];

      if (this.contents.pageTotal) arr.push("total");
      if (this.contents.pageSizes) arr.push("sizes");
      if (this.contents.pagePrevNext) {
        arr.push("prev");
        if (this.contents.pagePager) arr.push("pager");
        arr.push("next");
      }
      if (this.contents.pageJumper) arr.push("jumper");
      this.layouts = arr.join();
      this.contents.pageEachNum = 10;
    },

    init() {},
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: "id",
      };
      if (
        this.searchForm.username != "" &&
        this.searchForm.username != undefined
      ) {
        params["username"] = "%" + this.searchForm.username + "%";
      }
      this.$http({
        url: "users/page",
        method: "get",
        params: params,
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id, type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if (type != "info") {
        type = "else";
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type);
      });
    },
    // 查看评论
    // 下载
    download(file) {
      window.open(`${file}`);
    },
    // 删除
    deleteHandler(id) {
      var ids = id
        ? [Number(id)]
        : this.dataListSelections.map((item) => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http({
          url: "users/delete",
          method: "post",
          data: ids,
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.search();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
<style scoped>
.modern-list-container {
  --primary-color: #409eff;
  --success-color: #67c23a;
  --danger-color: #f56c6c;
  --warning-color: #e6a23c;
  --border-color: #ebeef5;
  --bg-base: #f5f7fa;
  --radius-md: 8px;
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.modern-list-content {
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  border: 1px solid var(--border-color);
}

.modern-search-form {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border-color);
}

.search-fields {
  display: flex;
  align-items: center;
  gap: 16px;
}

.modern-search-input {
  width: 280px;
  transition: all 0.3s ease;
}

.modern-search-input.prefix-icon::v-deep .el-input__inner {
  padding-left: 40px;
}

.modern-search-input.suffix-icon::v-deep .el-input__inner {
  padding-right: 40px;
}

.modern-search-btn {
  padding: 12px 28px;
  border-radius: 20px;
}

.action-toolbar {
  padding: 12px 24px;
  border-bottom: 1px solid var(--border-color);
}

.action-group {
  display: flex;
  gap: 12px;
}

.modern-action-btn {
  padding: 10px 18px;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.modern-action-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.modern-table-wrapper {
  padding: 0 24px 24px;
}

.modern-data-table::v-deep {
  --el-table-header-background-color: rgba(245, 247, 250, 0.9);
  --el-table-border-color: var(--border-color);
}

.modern-data-table::v-deep th {
  background: var(--el-table-header-background-color);
  backdrop-filter: blur(4px);
  font-weight: 600;
  color: var(--text-primary);
}

.modern-data-table::v-deep tr {
  transition: all 0.2s ease;
}

.modern-data-table::v-deep tr:hover {
  transform: translateX(4px);
  background: rgba(64, 158, 255, 0.03);
  box-shadow: 4px 0 12px rgba(64, 158, 255, 0.05);
}

.index-badge {
  display: inline-block;
  width: 28px;
  height: 28px;
  line-height: 28px;
  border-radius: 50%;
  background: rgba(64, 158, 255, 0.1);
  color: var(--primary-color);
  font-weight: 500;
}

.secret-text {
  font-family: monospace;
  letter-spacing: 2px;
}

.table-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.action-btn {
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.action-btn:hover {
  background: rgba(64, 158, 255, 0.08);
  color: var(--primary-color);
}

.modern-pagination {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
}

@media (max-width: 768px) {
  .search-fields {
    flex-direction: column;
  }

  .modern-search-input {
    width: 100%;
  }

  .action-group {
    flex-wrap: wrap;
  }

  .table-actions {
    flex-wrap: wrap;
    justify-content: center;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modern-list-content {
  animation: fadeIn 0.3s ease;
}
.el-input__prefix{
  display: flex;
  align-items: center;
  margin-top: 50px;
}
</style>
